package com.haoshuang.zhouzhoubang.ui.page.settled.autograph

import android.Manifest
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import com.haoshuang.zhouzhoubang.databinding.ActivityAutographBinding
import com.haoshuang.zhouzhoubang.ui.page.settled.collaborationcosts.CollaborationCostsActivity
import com.haoshuang.zhouzhoubang.utils.OSSUploadUtils
import com.haoshuang.zhouzhoubang.utils.PermissionHelper
import com.haoshuang.zhouzhoubang.utils.UploadSignUtil
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.ktx.runOnUIThread
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger


/**
 *  手写签名
 */
class AutographActivity : BaseVMActivity<AutographViewModel, ActivityAutographBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<AutographActivity>()
        }
    }

    override fun initView(savedInstanceState: Bundle?) {

        binding.titleBar.title = "签名"
        binding.titleBar.leftImageView().click { finish() }

        binding.tvClean.click {
            binding.llSignPanel.undo()
        }

        binding.tvSign.clickWithTrigger {
            if (binding.llSignPanel.bitmap == null) {
                toast("请先签名")
                return@clickWithTrigger
            }
            // 获取权限

            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {
                UploadSignUtil.requestPermission(this, binding.llSignPanel.bitmap)
                val path = UploadSignUtil.signatureFile?.path

                OSSUploadUtils.uploadFile("store/settled", path ?: "") {
                    start { runOnUIThread { showLoading() } }
                    error { clientException, serviceException ->
                        runOnUIThread {
                            dismissLoading()
                            toast("上传失败")
                        }
                    }
                    success {
                        runOnUIThread {
                            dismissLoading()
                            vm.contractSign(it)
                        }
                    }
                }
            } else {
                // 请求权限
                PermissionHelper.requestPermission(
                    mActivity,
                    "权限使用说明",
                    "用于存储签名文件",
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                ) {
                    if (it){
                        UploadSignUtil.requestPermission(this, binding.llSignPanel.bitmap)
                        val path = UploadSignUtil.signatureFile?.path

                        OSSUploadUtils.uploadFile("store/settled", path ?: "") {
                            start { runOnUIThread { showLoading() } }
                            error { clientException, serviceException ->
                                runOnUIThread {
                                    dismissLoading()
                                    toast("上传失败")
                                }
                            }
                            success {
                                runOnUIThread {
                                    dismissLoading()
                                    vm.contractSign(it)
                                }
                            }
                        }
                    }
                }
            }

        }

    }

    override fun initData() {

    }

    override fun startObserve() {
        vm.contractSignData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                toast("签名成功")
                // 跳转到支付页面
                setResult(200)
                finish()
            }
        }
    }

}